<?php
require('./constrl/lookPage.php');
pdlook();
require('../model/conn.php');
// 处理密码更新请求
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["uid"]) && isset($_POST["newpass"])) {
  $uid = intval($_POST["uid"]);
  $newpass = $_POST["newpass"];
  if ($newpass == "") {
    echo "<script>alert('密码不能为空');history.back();</script>";
    exit;
  }
  // 更新用户密码
  $sql = "UPDATE tbl_user SET uPass='$newpass' WHERE uid=$uid";
  $rows = exec_update($sql);
  if ($rows > 0) {
    echo "<script>alert('密码已更新');window.location.href='user_manage.php';</script>";
    exit;
  } else {
    echo "<script>alert('密码更新失败');window.location.href='user_manage.php';</script>";
    exit;
  }
}
// 查询所有用户
$users = exec_select("SELECT uid, uName, created_time FROM tbl_user");
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>说点吧 - 用户管理</title>
  <link rel="stylesheet" href="../style/css/bootstrap.min.css">
  <link rel="stylesheet" href="../style/css/adminIndex.css">
  <link rel="stylesheet" href="../style/css/font-face.css">
</head>

<body>
  <!-- 导航栏 -->
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <span class="navbar-brand">说点吧 后台管理</span>
      </div>
      <ul class="nav navbar-nav">
        <li><a href="index.php">后台首页</a></li>
        <li class="active"><a href="user_manage.php">用户管理</a></li>
        <li><a href="user_upgrade.php">账户升级</a></li>
        <li><a href="board_manage.php">板块管理</a></li>
        <li><a href="article_manage.php">管理文章</a></li>
          <li>
          <?php
          if (isset($_SESSION['hdb']) && trim($_SESSION['hdb']) != '') {
            echo "<span> $_SESSION[hdb]</span><a href='view/adminOut.php'>退出管理</a>";
          }
          ?>
        </li>
      </ul>
    </div>
  </nav>

  <div class="container">
    <h2>用户管理</h2>
    <!-- 用户列表表格 -->
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>用户ID</th>
          <th>用户名</th>
          <th>注册时间</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($users as $user) { ?>
          <tr>
            <td><?php echo $user['uid']; ?></td>
            <td><?php echo $user['uName']; ?></td>
            <td><?php echo $user['created_time']; ?></td>
            <td>
              <a href="?editUid=<?php echo $user['uid']; ?>" class="btn btn-sm btn-info">修改密码</a>
            </td>
          </tr>
        <?php } ?>
      </tbody>
    </table>

    <?php
    // 若点击了“修改密码”按钮，显示修改密码的表单
    if (isset($_GET['editUid'])) {
      $editUid = intval($_GET['editUid']);
      $editUser = null;
      foreach ($users as $u) {
        if ($u['uid'] == $editUid) {
          $editUser = $u;
          break;
        }
      }
      if ($editUser) {
        ?>
        <hr>
        <h4>修改用户 “<?php echo $editUser['uName']; ?>” 的密码</h4>
        <form method="post" class="form-inline">
          <input type="hidden" name="uid" value="<?php echo $editUser['uid']; ?>">
          <div class="form-group">
            <label for="newpass">新密码：</label>
            <input type="password" id="newpass" name="newpass" class="form-control" placeholder="输入新密码">
          </div>
          <button type="submit" class="btn btn-primary">保存</button>
          <a href="user_manage.php" class="btn btn-default">取消</a>
        </form>
        <?php
      }
    }
    ?>
  </div>

  <script src="../style/js/jquery.min.js"></script>
  <script src="../style/js/bootstrap.min.js"></script>
</body>

</html>